# coding:utf-8

from sqlalchemy import event

from .models import Pack, User
from .constant import UserTrainingStatus


@event.listens_for(Pack.bid_point, 'set', active_history=True)
def bid_sync_user_training_status(target, value, oldvalue, initiator):
    """
    created by chenhj at 2017/9/13
    当套餐的汇币发生改变时, 如果>=50, 则把用户移出养成队列
    """
    if value != oldvalue and value >= 50 and target.uid:
        user = User.get_by_id(target.uid)
        if user.training_status != UserTrainingStatus.NOT_TRAINING.value:
            user.training_status = UserTrainingStatus.NOT_TRAINING.value


@event.listens_for(Pack.dispatch_point, 'set', active_history=True)
def dispatch_sync_user_training_status(target, value, oldvalue, initiator):
    """
    created by chenhj at 2017/9/13
    当套餐的派单点发生改变时, 如果>0, 则把用户移出养成队列
    """
    if value != oldvalue and value > 0 and target.uid:
        user = User.get_by_id(target.uid)
        if user.training_status != UserTrainingStatus.NOT_TRAINING.value:
            user.training_status = UserTrainingStatus.NOT_TRAINING.value
